<?php

namespace App\Kuafu\Activity\Controller\Applet;

use App\Kuafu\Activity\Exceptions\ActivityException;
use App\Kuafu\Activity\Validate\ActivityNewValidate;
use App\Kuafu\Activity\Validate\ActivityValidate;
use App\Kuafu\Activity\Enum\ActivityCodeEnum;
use App\Kuafu\Activity\Service\ActivityService;
use App\Kuafu\User\Exceptions\UserException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ActivityController extends BaseController
{
    /**
     * 根据id获取详情
     * @param $request
     * @param $configId
     */
    public function getActivityById(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = ActivityValidate::getActivityById(['id' => $id]);
        if ($valid) {
            return $this->fail(ActivityCodeEnum::ERR_ACTIVITY_PARAM_VALID, $valid);
        }
        $info = ActivityService::getActivityById($this->shopId, $id);

        return $this->success($info);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     * @throws UserException
     */
    public function getActivityList(Request $request): JsonResponse
    {
        $validate['pageSize'] = $request->get('pageSize', 20);
        $validate['pageNum']  = $request->get('pageNum', 1);
        $valid                = ActivityValidate::getActivityPage($validate);
        if ($valid) {
            return $this->fail(ActivityCodeEnum::ERR_ACTIVITY_PARAM_VALID, $valid);
        }
        $lists = ActivityService::getAppletActivityList($request->header('brandId'), $validate['pageSize'],
            $validate['pageNum']);

        return $this->success($lists);
    }

    /**
     * 新增
     * @param $request
     * @return JsonResponse
     */
    public function addActivity(Request $request)
    {
        $valid = ActivityValidate::addActivity($request->all());
        if ($valid) {
            return $this->fail(ActivityCodeEnum::ERR_ACTIVITY_PARAM_VALID, $valid);
        }

        ActivityService::addActivity($request->all());
        return $this->success();
    }

    /**
     * 删除（软删除）
     * @param $request
     * @param $configId
     */
    public function delActivity(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = ActivityValidate::delActivity(['id' => $id]);
        if ($valid) {
            return $this->fail(ActivityCodeEnum::ERR_ACTIVITY_PARAM_VALID, $valid);
        }

        ActivityService::delActivity($id);
        return $this->success();
    }

    /**
     * 根据id修改信息
     * @param $request
     */
    public function setActivity(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = ActivityValidate::setActivity($request->all() + ['id' => $id]);
        if ($valid) {
            return $this->fail(ActivityCodeEnum::ERR_ACTIVITY_PARAM_VALID, $valid);
        }

        ActivityService::setActivity($id, $request->all());
        return $this->success();
    }

    /**
     * 上下架
     * @param $request
     */
    public function pathActivityStatus(Request $request, $id)
    {
        $status = $request->get('status');

        $id    = (int)$id;
        $valid = ActivityValidate::patchActivityStatus($request->all() + ['id' => $id]);
        if ($valid) {
            return $this->fail(ActivityCodeEnum::ERR_ACTIVITY_PARAM_VALID, $valid);
        }

        ActivityService::patchActivityStatus($id, $status);
        return $this->success();
    }

    /**
     * 活动详情
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ActivityException
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function detail(Request $request, $id): JsonResponse
    {
        ActivityNewValidate::detailValidate($request->merge(["id" => $id])->all());
        return $this->success(ActivityService::detail($this->shopId, $request->user()->userId, $id));
    }
}
